package com.xinxing.city.recovery.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.databinding.ViewDataBinding;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recovery.databinding.AdapterCategoryBinding;
import com.xinxing.city.recovery.entitys.BucketType;
import com.xinxing.city.recovery.entitys.Catalogs;
import com.xinxing.city.recovery.entitys.Category;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CategoryAdapter extends BaseDBAdapter<BucketType>{

    public Float yweight = 0f;
    public Integer bfb = 0;
    public String sy = "0";


    public CategoryAdapter(int itemLayoutRes, int variableId, Context context) {
        super(itemLayoutRes, variableId);

    }

    @Override
    public void bindItem(ViewDataBinding binding, BucketType item, int p) {
        super.bindItem(binding, item, p);
        AdapterCategoryBinding bind = (AdapterCategoryBinding) binding;


        updateWeight(bind,item);
        Log.e("TTT",item.getWeight()+"   dddddd");
    }

    //计算百分比
    public void updateWeight(AdapterCategoryBinding binding,BucketType dataitem) {

        float w = dataitem.getWeight()*1000;

        Log.e("TTT","weight "+w);
        String wu_c = dataitem.getCapacity();
        w = w < 0 ? 0:w;
        w = w > 100000? 100000 : w;
        yweight = w;
        //更新桶信息
        Float wu = 100.0f;
        if(wu_c != null && !wu_c.isEmpty()) wu = Float.parseFloat(dataitem.getCapacity());

        Float one1 = ((w/1000.00f)/wu);
        Float one2 = 0.00f;
        Integer one1Text = 0;
        if (one1 < 1) {
            one2 = 1 - one1;
            one1Text = (int) (one1 * 100);
        } else {
            one1 = 1f;
            one1Text = 100;
        }

        //剩余
        if (one1Text <= 0) one1Text = 0;
        Float sy1 = wu - (w/1000.00f);
        sy = String.format("%.1f", sy1);

//        TextView reText = findViewById(R.id.re_text);
        binding.reText.setText(one1Text+"%");
        bfb = one1Text;

//        RelativeLayout w1View = findViewById(R.id.re_per1);
//        LinearLayout w2View = findViewById(R.id.re_per2);

        LinearLayout.LayoutParams layoutParams1 = (LinearLayout.LayoutParams) binding.rePer1.getLayoutParams();
        LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) binding.rePer2.getLayoutParams();

        layoutParams1.weight = one1;
        layoutParams2.weight = one2;

        binding.rePer1.setLayoutParams(layoutParams1);
        binding.rePer2.setLayoutParams(layoutParams2);

    }

}
